package com.wy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.wy.bean.Brand;
import com.wy.bean.Shop;
import com.wy.service.ShopService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class ShopController {
    @Resource
    private ShopService shopService;
    @RequestMapping("head")
    public String head(){
        return "head";
    }
    @RequestMapping("brand")
    public String brand(Model model){
        model.addAttribute("dqpage","brand");
        return "brand";
    }
    @RequestMapping("addbrand")
    public String addbrand(Model model, Brand brand, MultipartFile file){
        if(file!=null && file.getSize()>0){
            String originalFilename = file.getOriginalFilename();
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            String s = UUID.randomUUID().toString()+substring;
            try {
                file.transferTo(new File("D:\\tp\\"+s));
                brand.setPic("/tp/"+s);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
            shopService.insert(brand);
        return "redirect:brand.do";
    }
    @RequestMapping("addShop")
    public String addShop(Model model){
        List<Brand> list = shopService.getlist();
        model.addAttribute("dqpage","addShop");
        model.addAttribute("list",list);
        return "addShop";
    }
    @RequestMapping("saveShop")
    public String saveShop(Model model, Shop shop, MultipartFile file){
        if(file!=null && file.getSize()>0){
            String originalFilename = file.getOriginalFilename();
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            String s = UUID.randomUUID().toString()+substring;
            try {
                file.transferTo(new File("D:\\tp\\"+s));
                shop.setImg("/tp/"+s);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        shopService.insertbth(shop);
        return "redirect:addShop.do";
    }
    @RequestMapping("shoplist")
    public String shoplist(Model model,String bid,
     @RequestParam(defaultValue = "1") int pageNum,
     @RequestParam(defaultValue = "3") int pageSize
                           ){
        if(StringUtil.isNotEmpty(bid)){
            PageHelper.startPage(pageNum,pageSize);
            List<Shop> shoplist = shopService.getShoplist(bid);
            PageInfo<Shop> pageInfo = new PageInfo<>(shoplist);
            model.addAttribute("dqpage","shoplist");
            model.addAttribute("bid",bid);
            model.addAttribute("pageInfo",pageInfo);
        }
        List<Brand> list = shopService.getlist();
        model.addAttribute("list",list);
        return "shoplist";
    }
}
